/*
 * MainWindow.cpp
 *
 *  Created on: 20.09.2012
 *      Author: Sergey
 */

#include "MainWindow.h"
#include "DownloadManagerTable.h"
#include "Download.h"
#include "Settings.h"
#include "SettingsWidget.h"

#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QFileInfo>
#include <QDebug>
#include <QtGui>
#include <QCloseEvent>

MainWindow::MainWindow() {
	DownloadManagerTable *dmt = new DownloadManagerTable(this);
	setCentralWidget(dmt);
	connect(dmt, SIGNAL(isDownloadSelected(bool)),
			this, SLOT(setRemoveDownloadActionEnabled(bool)));
	connect(dmt, SIGNAL(isRunningDownloadSelected(bool)),
			this, SLOT(setStopDownloadActionEnabled(bool)));
	connect(dmt, SIGNAL(isNewDownloadSelected(bool)),
			this, SLOT(setStartDownloadActionEnabled(bool)));
	connect(this, SIGNAL(settingsChanged()), dmt, SLOT(tryToStartPendingDownloads()));

	QAction *newAct = new QAction(QIcon(":/manage/new.png"), tr("&New download"), this);
	newAct->setShortcut(tr("Ctrl+N"));
	newAct->setStatusTip(tr("Create a new download"));
	connect(newAct, SIGNAL(triggered()), this, SLOT(showNewDownloadDialog()));

	removeAct = new QAction(QIcon(":/manage/remove.png"), tr("&Remove"), this);
	removeAct->setShortcut(tr("Ctrl+X"));
	removeAct->setStatusTip(tr("Remove selected download"));
	removeAct->setEnabled(false);
	connect(removeAct, SIGNAL(triggered()), dmt, SLOT(removeSelectedDownload()));

	stopAct = new QAction(QIcon(":/manage/stop.png"), tr("&Stop"), this);
	stopAct->setShortcut(tr("Ctrl+S"));
	stopAct->setStatusTip(tr("Stop selected download"));
	stopAct->setEnabled(false);
	connect(stopAct, SIGNAL(triggered()), dmt, SLOT(stopSelectedDownload()));

	startAct = new QAction(QIcon(":/manage/start.png"), tr("&Start"), this);
	startAct->setShortcut(tr("Ctrl+R"));
	startAct->setStatusTip(tr("Start selected download"));
	startAct->setEnabled(false);
	connect(startAct, SIGNAL(triggered()), dmt, SLOT(startSelectedDownload()));

	QAction *exitAct = new QAction(tr("&Exit"), this);
	exitAct->setShortcut(tr("Ctrl+Q"));
	exitAct->setStatusTip(tr("Exit from application"));
	connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

	QAction *settingsAct = new QAction(QIcon(":/manage/settings.png"), tr("&Settings"), this);
	settingsAct->setStatusTip(tr("Application settings"));
	connect(settingsAct, SIGNAL(triggered()), this, SLOT(openSettings()));


	QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
	fileMenu->addAction(newAct);
	fileMenu->addSeparator();
	fileMenu->addAction(startAct);
	fileMenu->addAction(stopAct);
	fileMenu->addAction(removeAct);
	fileMenu->addSeparator();
	fileMenu->addAction(exitAct);

	QMenu *toolsMenu = menuBar()->addMenu(tr("&Tools"));
	toolsMenu->addAction(settingsAct);

	QToolBar *toolBar = new QToolBar(this);
	toolBar->setMovable(false);
	toolBar->setFloatable(false);
	toolBar->addAction(newAct);
	toolBar->addSeparator();
	toolBar->addAction(startAct);
	toolBar->addAction(stopAct);
	toolBar->addAction(removeAct);
	toolBar->addSeparator();
	toolBar->addAction(settingsAct);

	addToolBar(Qt::TopToolBarArea, toolBar);
}

void MainWindow::showNewDownloadDialog()
{
	NewDownloadDialog *downloadDialog = new NewDownloadDialog();

	if (downloadDialog->exec() == QDialog::Accepted) {
		QString downloadLink = downloadDialog->getDownloadLink();
		QString savingPath = downloadDialog->getSavingPath();
		int priority = downloadDialog->getPriority();

		qobject_cast<DownloadManagerTable*>(centralWidget())->
				createDownload(downloadLink, savingPath, priority);
	}

	delete downloadDialog;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	emit save();
	event->ignore();

	if (!Settings::getAskBeforeExit() ||
		QMessageBox::question(this, tr("Exit"),
			tr("Are you sure you want to exit? "
					"All not finished downloads progress will be lost."),
			QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
			== QMessageBox::Yes) {
		event->accept();
	}
}

void MainWindow::setStartDownloadActionEnabled(bool enabled)
{
	startAct->setEnabled(enabled);
}

void MainWindow::setStopDownloadActionEnabled(bool enabled)
{
	stopAct->setEnabled(enabled);
}

void MainWindow::setRemoveDownloadActionEnabled(bool enabled)
{
	removeAct->setEnabled(enabled);
}

void MainWindow::openSettings()
{
	SettingsWidget *settings = new SettingsWidget;
	if (settings->exec()) {
		emit settingsChanged();
	}
	delete settings;
}
